package com.lyj.io.module.sys.controller;

import com.lyj.io.annotation.SysLogAnnotation;
import com.lyj.io.common.constant.Constant;
import com.lyj.io.common.utils.R;
import com.lyj.io.common.dto.IdsDto;
import com.lyj.io.module.sys.dto.MenuDto;
import com.lyj.io.module.sys.dto.PermissionDto;
import com.lyj.io.module.sys.entity.SysMenu;
import com.lyj.io.module.sys.entity.SysPermission;
import com.lyj.io.module.sys.service.SysMenuService;
import com.lyj.io.module.sys.service.SysPermissionService;
import com.lyj.io.module.sys.service.SysUserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜单相关api
 * @author: 李亚杰
 * @version: 1.0
 * @date: 2021/9/13 16:08
 */
@Api(value = "SysMenuController", tags = "菜单相关api")
@RestController
@RequestMapping("/api/v1/menu")
public class SysMenuController {
    @Autowired
    SysPermissionService sysPermissionService;

    @Autowired
    SysUserRoleService sysUserRoleService;

    @Autowired
    SysMenuService sysMenuService;

    /**
     * 根据角色ID查询菜单
     * @param
     * @return com.lyj.io.common.utils.R
     * @author: liyajie
     * @date: 2021/11/15 18:14
     * @exception:
     * @update:
     * @updatePerson:
     **/
    @SysLogAnnotation("根据角色ID查询菜单")
    @ApiOperation(value = "根据角色ID查询菜单", notes = "根据角色ID查询菜单")
    @PostMapping("/getMenuListByRoleId")
    public R getMenuListByRoleId(@RequestBody MenuDto menuDto) {
        // 查询所有的菜单
        List<SysMenu> sysMenuList = sysMenuService.getUserMenuList(menuDto.getUserId());
        // 查询当前角色拥有的菜单
        SysPermission sysPermission = sysPermissionService.getPermissionsByRoleId(menuDto.getRoleId());
        if (null != sysPermission) {
            for (SysMenu sysMenu:sysMenuList) {
                if (sysPermission.getPermission().contains(sysMenu.getMenuId())) {
                    sysMenu.setSelected(true);
                } else {
                    sysMenu.setSelected(false);
                }
                if (null != sysMenu.getChildren() && sysMenu.getChildren().size() > 0) {
                    sysMenu.getChildren().forEach((childrenMenu) -> {
                        SysMenu itemMenu = (SysMenu) childrenMenu;
                        if (sysPermission.getPermission().contains(itemMenu.getMenuId())) {
                            itemMenu.setSelected(true);
                        } else {
                            itemMenu.setSelected(false);
                        }
                    });
                }
            }
        }
        return R.ok().put("data", sysMenuList);
    }

    /**
     * 查询所有菜单
     * @param
     * @return com.lyj.io.common.utils.R
     * @author: liyajie
     * @date: 2021/11/15 18:14
     * @exception:
     * @update:
     * @updatePerson:
     **/
    @SysLogAnnotation("查询所有菜单")
    @ApiOperation(value = "查询所有菜单", notes = "查询所有菜单")
    @GetMapping("/getMenuList")
    public R getMenuList() {
        // 查询所有的菜单
        List<SysMenu> sysMenuList = sysMenuService.getUserMenuList(Constant.SUPER_ADMIN);
        return R.ok().put("data", sysMenuList);
    }

    /**
     * 菜单授权
     * @param
     * @return com.lyj.io.common.utils.R
     * @author: liyajie
     * @date: 2021/11/15 18:14
     * @exception:
     * @update:
     * @updatePerson:
     **/
    @SysLogAnnotation("菜单授权")
    @ApiOperation(value = "菜单授权", notes = "菜单授权")
    @PostMapping("/grantMenuToRole")
    public R grantMenuToRole(@RequestBody PermissionDto permissionDto) {
        SysPermission sysPermission = new SysPermission();
        sysPermission.setPermission(permissionDto.getPermission().stream().map(String::valueOf).collect(Collectors.joining(",")));
        sysPermission.setCreater(permissionDto.getCreater());
        sysPermission.setRoleId(permissionDto.getRoleId());
        sysMenuService.grantMenuToRole(sysPermission);
        return R.ok();
    }

    /**
     * 新增菜单
     * @param
     * @return com.lyj.io.common.utils.R
     * @author: liyajie
     * @date: 2021/11/15 18:14
     * @exception:
     * @update:
     * @updatePerson:
     **/
    @SysLogAnnotation("新增菜单")
    @ApiOperation(value = "新增菜单", notes = "新增菜单")
    @PostMapping("/addMenu")
    public R addMenu(@RequestBody SysMenu sysMenu) {
        sysMenuService.save(sysMenu);
        return R.ok();
    }

    /**
     * 删除菜单
     * @param
     * @return com.lyj.io.common.utils.R
     * @author: liyajie
     * @date: 2021/11/15 18:14
     * @exception:
     * @update:
     * @updatePerson:
     **/
    @SysLogAnnotation("删除菜单")
    @ApiOperation(value = "删除菜单", notes = "删除菜单")
    @PostMapping("/deleteMenu")
    public R deleteMenu(@RequestBody IdsDto idsDto) {
        String ids = idsDto.getIds();
        sysMenuService.removeByIds(Arrays.asList(ids.split(",")));
        return R.ok();
    }

    /**
     * 修改菜单
     * @param sysMenu
     * @return com.lyj.io.common.utils.R
     * @author: liyajie
     * @date: 2021/10/8 15:55
     * @exception:
     * @update:
     * @updatePerson:
     **/
    @SysLogAnnotation("修改菜单")
    @ApiOperation(value = "修改菜单", notes = "修改菜单")
    @PostMapping("/updateMenu")
    public R updateMenu(@RequestBody SysMenu sysMenu) {
        sysMenuService.updateById(sysMenu);
        return R.ok();
    }
}
